﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Partial Class CoBroke
    Inherits System.Web.UI.Page

#Region " Properties "

    Public Property CurrentPage() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPage As Object = ViewState("_CurrentPage")
            If objPage Is Nothing Then
                Return 0
            Else
                'default to showing the first page
                Return CInt(objPage)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_CurrentPage") = value
        End Set
    End Property

    Public Property PageSize() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPageSize As Object = ViewState("_PageSize")
            If objPageSize Is Nothing Then
                Return 10
            Else
                'default to showing the first page
                Return CInt(objPageSize)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_PageSize") = value
        End Set
    End Property

#End Region

#Region " Paging and Page Size "

    Protected Sub lnkHead10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead10Item.Click
        PageSize = 10
         LoadCoBroke()
    End Sub

    Protected Sub lnkHead25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead25Item.Click
        PageSize = 25
        LoadCoBroke()
    End Sub

    Protected Sub lnkHead100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead100Item.Click
        PageSize = 100
        LoadCoBroke()
    End Sub

    Protected Sub lnkHead150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead150Item.Click
        PageSize = 150
        LoadCoBroke()
    End Sub

    Protected Sub lnkFoot10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot10Item.Click
        PageSize = 10
       LoadCoBroke()
    End Sub

    Protected Sub lnkFoot25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot25Item.Click
        PageSize = 25
        LoadCoBroke()
    End Sub

    Protected Sub lnkFoot100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot100Item.Click
        PageSize = 100
        LoadCoBroke()
    End Sub

    Protected Sub lnkFoot150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot150Item.Click
        PageSize = 150
        LoadCoBroke()
    End Sub

    Protected Sub btnMoveFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveFirst.Click
        CurrentPage = 0
        txtPageNo.Text = CurrentPage + 1
         LoadCoBroke()
    End Sub

    Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        If CurrentPage > 0 Then
            CurrentPage = CurrentPage - 1
            txtPageNo.Text = CurrentPage + 1
            LoadCoBroke()
        End If
    End Sub

    Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click
        If CInt(txtPageNo.Text < 1) Then
            txtPageNo.Text = 1
        End If

        If CInt(txtPageNo.Text > CInt(lblMaxPageCount.Text)) Then
            txtPageNo.Text = CInt(lblMaxPageCount.Text) + 1
        End If

        CurrentPage = CInt(txtPageNo.Text) - 1
        LoadCoBroke()

    End Sub

    Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If CurrentPage < CInt(lblMaxPageCount.Text) Then
            CurrentPage = CurrentPage + 1
            txtPageNo.Text = CurrentPage + 1
             LoadCoBroke()
        End If
    End Sub

    Protected Sub btnMoveLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveLast.Click
        CurrentPage = CInt(lblMaxPageCount.Text)
        txtPageNo.Text = CurrentPage + 1
        LoadCoBroke()
    End Sub

#End Region

    Private Sub LoadCoBroke()

        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable

        'edited by minginn @ 28/6/2011 - to load the listing according to search criteria after edit but not after save
        Try
            objDB.OpenConnection()
            If txtSearch.Text = "" Then
                objDB.dbVariable.SQLString = "SELECT CoBrokeId,Name,AgencyName,Phone,Mobile,Fax,Email FROM AW_CoBroke WHERE StatusDelete=0" & lblhidSortType.Text
                objDB.FillData(TempDT, "AW_CoBroke", Database.SQLCommandType.NormalString)
            Else
                objDB.dbVariable.SQLString = "SELECT CoBrokeId,Name,AgencyName,Phone,Mobile,Fax,Email FROM AW_CoBroke WHERE StatusDelete=0 AND Name LIKE @Name" & lblhidSortType.Text
                objDB.AddParameter("Name", Data.SqlDbType.NVarChar, "%" & txtSearch.Text.Trim & "%")
                objDB.FillData(TempDT, "AW_CoBroke", Database.SQLCommandType.NormalString)
            End If
            objDB.CloseConnection()
        Catch ex As Exception

        Finally
            If (objDB IsNot Nothing) Then
                objDB.CloseConnection()
            End If
        End Try
        'Creating a Paged Datasouce
        Dim objPaged As New PagedDataSource
        objPaged.DataSource = TempDT.DefaultView
        objPaged.AllowPaging = True
        objPaged.PageSize = PageSize
        objPaged.CurrentPageIndex = CurrentPage

        lblHeadNoOfItem.Text = TempDT.Rows.Count
        lblFootNoOfItem.Text = TempDT.Rows.Count

        repCoBroke.DataSource = objPaged
        repCoBroke.DataBind()

        lblMaxPageCount.Text = objPaged.PageCount - 1
        lblTotalPage.Text = " of " & objPaged.PageCount
    End Sub

    Protected Sub imgNewCoBroke_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgNewCoBroke.Click
        ClearAllField()
        mpeEntry.Show()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Session("StaffId") Is Nothing Then
            Response.Redirect("Default.aspx")
        ElseIf Session.Item("StaffType").ToString = "2" Then
            Response.Redirect("Default.aspx")
        Else
            lblStaffUID.Text = Session("StaffId").ToString
        End If

        If Not Page.IsPostBack Then
            LoadCoBroke()
        End If
    End Sub

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        LoadCoBroke()
    End Sub

    Protected Sub repCoBroke_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repCoBroke.ItemCommand

        If e.CommandName.ToUpper = "EDITITEM" Then
            txtCoBroke.Text = CType(repCoBroke.Items(e.CommandArgument).FindControl("lblRepCoBroke"), Label).Text
            txtAgencyName.Text = CType(repCoBroke.Items(e.CommandArgument).FindControl("lblRepAgencyName"), Label).Text
            txtPhone.Text = CType(repCoBroke.Items(e.CommandArgument).FindControl("lblRepPhone"), Label).Text
            txtMobile.Text = CType(repCoBroke.Items(e.CommandArgument).FindControl("lblRepMobile"), Label).Text
            txtFax.Text = CType(repCoBroke.Items(e.CommandArgument).FindControl("lblRepFax"), Label).Text
            txtEmail.Text = CType(repCoBroke.Items(e.CommandArgument).FindControl("lblRepEmail"), Label).Text
            lblCoBrokeUID.Text = CType(repCoBroke.Items(e.CommandArgument).FindControl("lblRepCoBrokeUID"), Label).Text
            mpeEntry.Show()
        Else
            'Begin Delete
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
            Try
                objDB.OpenConnection()
                objDB.dbVariable.SQLString = "UPDATE AW_CoBroke SET StatusDelete=1 WHERE CoBrokeId=@CoBrokeId"
                objDB.AddParameter("CoBrokeId", Data.SqlDbType.UniqueIdentifier, New Guid(CType(repCoBroke.Items(e.CommandArgument).FindControl("lblRepCoBrokeUID"), Label).Text))
                objDB.ExecuteQuery()
                objDB.CloseConnection()
            Catch ex As Exception

            Finally
                If (objDB IsNot Nothing) Then
                    objDB.CloseConnection()
                End If
            End Try
            lblMsgSuccessful.Text = "Co-Broke Deleted"
            lblMsgSuccessful.Visible = True
            LoadCoBroke()
        End If
    End Sub

    Private Sub ClearAllField()
        txtCoBroke.Text = String.Empty
        txtAgencyName.Text = String.Empty
        txtMobile.Text = String.Empty
        txtPhone.Text = String.Empty
        txtFax.Text = String.Empty
        txtEmail.Text = String.Empty
        lblCoBrokeUID.Text = String.Empty
        divError.Visible = False
        divSuccessful.Visible = False
    End Sub

    Private Sub ClearFilterField()
        txtSearch.Text = String.Empty
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click

        If lblCoBrokeUID.Text = "" Then
            'New
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
            Try
                objDB.OpenConnection()
                objDB.dbVariable.SQLString = "INSERT INTO AW_CoBroke (CoBrokeId,Name,AgencyName,Phone,Mobile,Fax,Email,CreateDate,StatusDelete) " & _
                                            "VALUES (@CoBrokeId,@Name,@AgencyName,@Phone,@Mobile,@Fax,@Email,@CreateDate,@StatusDelete)"
                objDB.AddParameter("CoBrokeId", Data.SqlDbType.UniqueIdentifier, Guid.NewGuid)
                objDB.AddParameter("Name", Data.SqlDbType.NVarChar, txtCoBroke.Text.Trim.ToUpper)
                objDB.AddParameter("AgencyName", Data.SqlDbType.NVarChar, txtAgencyName.Text.Trim.ToUpper)
                objDB.AddParameter("Phone", Data.SqlDbType.NVarChar, txtPhone.Text.Trim.ToUpper)
                objDB.AddParameter("Mobile", Data.SqlDbType.NVarChar, txtMobile.Text.Trim.ToUpper)
                objDB.AddParameter("Fax", Data.SqlDbType.NVarChar, txtFax.Text.Trim.ToUpper)
                objDB.AddParameter("Email", Data.SqlDbType.NVarChar, txtEmail.Text.Trim.ToUpper)
                objDB.AddParameter("CreateDate", Data.SqlDbType.DateTime, Now)
                objDB.AddParameter("StatusDelete", Data.SqlDbType.TinyInt, 0)
                objDB.ExecuteQuery()
                objDB.CloseConnection()
            Catch ex As Exception

            Finally
                If (objDB IsNot Nothing) Then
                    objDB.CloseConnection()
                End If
            End Try
            ClearAllField()
            ClearFilterField()
            divSuccessful.Visible = True
            lblMsgSuccessful.Text = "Co-Broke Successfully Inserted"
            LoadCoBroke()
        Else
            'Edit
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))

            objDB.OpenConnection()
            objDB.dbVariable.SQLString = "UPDATE AW_CoBroke SET Name=@Name,AgencyName=@AgencyName,Phone=@Phone,Mobile=@Mobile,Fax=@Fax,Email=@Email WHERE CoBrokeId=@CoBrokeId"
            objDB.AddParameter("CoBrokeId", Data.SqlDbType.UniqueIdentifier, New Guid(lblCoBrokeUID.Text))
            objDB.AddParameter("Name", Data.SqlDbType.NVarChar, txtCoBroke.Text.Trim.ToUpper)
            objDB.AddParameter("AgencyName", Data.SqlDbType.NVarChar, txtAgencyName.Text.Trim.ToUpper)
            objDB.AddParameter("Phone", Data.SqlDbType.NVarChar, txtPhone.Text.Trim.ToUpper)
            objDB.AddParameter("Mobile", Data.SqlDbType.NVarChar, txtMobile.Text.Trim.ToUpper)
            objDB.AddParameter("Fax", Data.SqlDbType.NVarChar, txtFax.Text.Trim.ToUpper)
            objDB.AddParameter("Email", Data.SqlDbType.NVarChar, txtEmail.Text.Trim.ToUpper)
            objDB.ExecuteQuery()
            objDB.CloseConnection()
            ClearAllField()
            divSuccessful.Visible = True
            lblMsgSuccessful.Text = "Co-Broke Successfully Updated"
            LoadCoBroke()
        End If
    End Sub

    Private Function SortType(ByVal strSortType As String) As String
        Dim strSort As String
        If Right(strSortType, 1) = "▼" Then
            strSort = "ASC"
        Else
            strSort = "DESC"
        End If
        Return strSort
    End Function

    Protected Sub lnkCoBroke_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkCoBroke.Click
        If lnkCoBroke.Text = "Co-Broke▼" Then
            lnkCoBroke.Text = "Co-Broke▲"
        Else
            lnkCoBroke.Text = "Co-Broke▼"
        End If

        lblhidSortType.Text = " ORDER BY Name " & SortType(lnkCoBroke.Text) & ", AgencyName " & SortType(lnkAgencyName.Text) & _
        ", Mobile " & SortType(lnkMobile.Text)
        LoadCoBroke()
    End Sub

    Protected Sub lnkAgencyName_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkAgencyName.Click
        If lnkAgencyName.Text = "Agency Name▼" Then
            lnkAgencyName.Text = "Agency Name▲"
        Else
            lnkAgencyName.Text = "Agency Name▼"
        End If

        lblhidSortType.Text = " ORDER BY AgencyName " & SortType(lnkAgencyName.Text) & ", Name " & SortType(lnkCoBroke.Text) & _
        ", Mobile " & SortType(lnkMobile.Text)
        LoadCoBroke()
    End Sub

    Protected Sub lnkMobile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkMobile.Click
        If lnkMobile.Text = "Mobile▼" Then
            lnkMobile.Text = "Mobile▲"
        Else
            lnkMobile.Text = "Mobile▼"
        End If

        lblhidSortType.Text = " ORDER BY Mobile " & SortType(lnkMobile.Text) & ", Name " & SortType(lnkCoBroke.Text) & _
        ", AgencyName " & SortType(lnkAgencyName.Text)
        LoadCoBroke()
    End Sub

    Protected Sub repCoBroke_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles repCoBroke.ItemDataBound
        If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
            CType(e.Item.FindControl("cbeDelete"), Object).confirmText = "Are you sure you want to delete " & CType(e.Item.FindControl("lblRepCobroke"), Label).Text
        End If
    End Sub
End Class
